/**
 * 建造者
 * 创建一个快餐套餐
 */

 // 纸盒
class Wrapper {
    pack() {
        return "Wrapper";
    }
}
// 瓶子
class Bottle {
    pack() {
        return "Bottle";
    }
}
// 汉堡需要纸盒包住
class Burger {
    packing() {
        return new Wrapper();
    }
}
// 冷饮需要瓶子装
class ColdDrink {
    packing() {
        return new Bottle();
    }
}
//蔬菜汉堡
class VegBurger extends Burger{
    price(){
        return 1.2
    }
    name(){
        return 'Veg Burger'
    }
}
//鸡肉宝
class ChickenBurger extends Burger {
    price() {
        return 50.5;
    }
    name() {
        return "Chicken Burger";
    }
}
// 可口
class Coke extends ColdDrink {
    price() {
       return 30.0;
    }
    name() {
       return "Coke";
    }
}
// 百事
class Pepsi extends ColdDrink {
    price() {
       return 35.0;
    }
    name() {
       return "Pepsi";
    }
}

//套餐
class Meal{
    constructor(){
        const items = []

        Reflect.defineProperty(this,'items',{
            get:()=>{
                return items
            }
        })
    }
    addItem(item){
        this.items.push(item)
    }
    getCost(){
        return this.items.reduce((pre,cur)=>{
           return pre + cur.price() 
        },0)
    }
    showItems(){
        this.items.forEach(v=>{
            const name = v.name()
            const price = v.price()
            const wrapper = v.packing().pack()
            console.log(`${name},${price},${wrapper}`)
        })
    }
}

//建造者
class MealBuilder{
    prepareVegMeal(){
        const meal = new Meal()
        meal.addItem(new VegBurger())
        meal.addItem(new Coke())
        return meal
    }
    prepareChickenMeal(){
        const meal = new Meal()
        meal.addItem(new ChickenBurger())
        meal.addItem(new Pepsi())
        return meal    
    }
}
//测试
const mealBuilder = new MealBuilder();
const vegMeal = mealBuilder.prepareVegMeal();
console.log("Veg Meal");
vegMeal.showItems();
console.log("Total Cost: " +vegMeal.getCost());

const nonVegMeal = mealBuilder.prepareChickenMeal();
console.log("\nChicken Meal");
nonVegMeal.showItems();
console.log("Total Cost: " +nonVegMeal.getCost());